<?php

class Admin_user_model extends Base_Model
{
    const ADMIN_USER_TABLE = 'hy_admin_user';
    const ADMIN_ROLE_TABLE = 'hy_admin_role';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 检测登陆状态
     *
     * @return boolean
     */
    public function get_login_status()
    {
        $is_login       = FALSE;
        $admin_username = $this->session->userdata('hy_admin_username') ? $this->session->userdata('hy_admin_username') : "";
        if (!empty($admin_username)) {
            $is_login = TRUE;
        }
        return $is_login;
    }


    /**
     * 检查用户是否存在
     * @author  KevinBai
     * @param   string $name 用户名
     * @param   int $type 类型(0:用户名,1:邮箱)
     * @return  boolean
     */
    public function check_user($username)
    {
        //验证用户名是否存在
        $this->db->where("username", $username);
        $res = $this->db->get(self::ADMIN_USER_TABLE);
        if ($res->num_rows() == 0) {
            return FALSE;
        }
        return TRUE;
    }

    /**
     * 检查邮箱是否存在
     * @author  KevinBai
     * @param   string $name 用户名
     * @param   int $type 类型(0:用户名,1:邮箱)
     * @return  boolean
     */
    public function check_email($email)
    {
        //验证用户名是否存在
        $this->db->where("email", $email);
        $res = $this->db->get(self::ADMIN_USER_TABLE);
        if ($res->num_rows() == 0) {
            return FALSE;
        }
        return TRUE;
    }

    /**
     * 重置密码
     * @param int $uid
     * @return string|boolean
     */
    function reset_password($uid, $newpassword)
    {
        $this->load->helper('md6');
        $uid         = intval($uid);
        $newpassword = md6($newpassword);
        $sql         = "update tr_admin_user set password='{$newpassword}' where id={$uid}";
        return $this->db->query($sql);
    }


    /**
     * 根据uid获取用户信息
     */
    public function get_userinfo($uid, $login_name)
    {
        if (!empty($uid)) {
            $this->db->where('id', $uid);
        }
        if (!empty($login_name)) {
            if (stripos($login_name, "@") !== FALSE) {
                $this->db->where('email', $login_name);
            } else {
                $this->db->where('username', $login_name);
            }
        }
        $query = $this->db->get(self::ADMIN_USER_TABLE);
        return $query->row();
    }

    /**
     * 检测登陆账号
     *
     * @param string $username
     * @param string $password
     * @return array
     */
    public function check_account($username = '', $password = '')
    {
        $arr_res = array();

        $username = trim(addslashes($username));
        $password = trim(addslashes($password));

        $password = md5(md5($password));

        $where = " ";
        if (stripos($username, "@") !== FALSE) {
            //$this->db->where('email', $username);
            $where .= " and user.email = '{$username}'";
        } else {
            //$this->db->where('username', $username);
            $where .= " and user.username = '{$username}'";
        }

        $where .= " and user.password = '{$password}'";
        //$this->db->where('password', $password);

        $sql   = "SELECT `user`.*,role.`name` role_name,role.power from " . self::ADMIN_USER_TABLE . " as user ," . self::ADMIN_ROLE_TABLE . " as role where `user`.roleid=role.id and `user`.isDel=0" . $where;
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $row = $query->row_array();
            if ($row['status'] != 1) {
                $arr_res = array('status' => 1, 'message' => '用户被禁用');
            } else {
                //获取用户权限
                //将用户信息写入session
                $_SESSION['hy_admin_id']       = $row['id'];
                $_SESSION['hy_admin_username'] = $row['username'];
                $_SESSION['adminUserInfo']     = $row;
                //更新用户登陆日志
                $login_info = array(
                    'lastlogin' => time(),
                    'lastip'    => $this->input->ip_address(),
                );
                $this->db->where('id', $row['id']);
                $this->db->update(self::ADMIN_USER_TABLE, $login_info);
                $arr_res = array('status' => 0, 'message' => '', 'id' => $row['id']);
            }
        } else {
            $arr_res = array('status' => 2, 'message' => '用户名或密码错误');
        }
        return $arr_res;
    }

    /**
     *
     * 通过用户email获取用户信息
     */
    public function get_userinfo_by_email($email)
    {
        $this->db->where('email', $email);
        $query = $this->db->get('tr_admin_user');
        if ($query->num_rows() > 0) {
            $res = $query->row_array();
            return $res;
        }
        return null;
    }


}

/* End of file School_user_model.php */
/* Location: ./application/models/school/school_user_model.php */